package cc.bookcloud.tool.ocr.common.version;
/*==========================================================================
 * Copyright (C) Wit2Cloud Co.,Ltd
 * All Rights Reserved.
 * Created By 开源学社
 ==========================================================================*/

import com.alibaba.fastjson.JSON;
import cc.bookcloud.tool.ocr.common.enum1.SystemInfoEnum;
import cc.bookcloud.tool.ocr.common.sqlite.Sqlite3Client;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author John Goo
 * @version 1.0
 * @ClassName: VersionManager
 * @Desc: 版本管理嚣
 * @history v1.0
 */
@Slf4j
@Configuration
public class VersionManager {


    @Resource
    private Sqlite3Client sqlite3Client;


    // 版本MAP
    private Map<String, List<String>> versionMap = new ConcurrentHashMap<>();

    public synchronized String fetchVersion(SystemInfoEnum systemInfoEnum) {
        List<String> versionList = versionMap.get(systemInfoEnum.getIdentify());
        String newVersion = null;
        if (versionList != null && versionList.size() > 0) {
            // 从大到小排序数组
            String version = versionList.get(0);
            // 自动生成,只能是末段,V2.1.9
            int last = version.lastIndexOf(".");
            String firstPart = version.substring(0, last);
            String lastPart = version.substring(last + 1, version.length());
            Integer end = Integer.valueOf(lastPart) + 1;
            newVersion = firstPart + "." + end;
        } else {
            newVersion = "V1.0.1";
        }
        return newVersion;
    }



    public synchronized void updateVersion(String identify, String version) {
       // String identify = systemInfoEnum.getIdentify();
        List<String> versionList = versionMap.get(identify);
        if (versionList != null) {
            versionList.add(0, version);
        } else {
            // 重新创建一个新List
            List list = new ArrayList();
            list.add(version);
            versionMap.put(identify, list);
        }

    }

   // @PostConstruct
    public void init() {
        for (SystemInfoEnum item : SystemInfoEnum.values()) {
            // 系统identify
            String identify = item.getIdentify();
            String type = item.getType();
            // 过滤应用类
            if ("app".equalsIgnoreCase(type)) {
                // 查询sqlite
                String sql = "select APP_VERSION from RELEASE_CODE_PACKAGE where APP_IDENTIFY=? order by APP_VERSION DESC limit 20";
                try {
                    List<String> versionList = sqlite3Client.use().query(sql, String.class, identify);
                    versionMap.put(identify, versionList);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("3");
        list.add("2");
        list.add("1");
        System.out.println("(1)" + JSON.toJSONString(list));
        // 追加1个
        list.add(0, "4");
        System.out.println("(2)" + JSON.toJSONString(list));
        System.out.println(":" + list.get(0));
    }

}
